//
//  YWLedgerPostTroubleTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/12/17.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWLedgerPostTroubleTableViewCell.h"
//#import "UILabel+AttributedText.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"
#import "YWHandler.h"
#import "YWMission.h"
#import "YWBuilding.h"
#import "YWBuildingManager.h"
#import "YWEquipmentManager.h"
#import "YWMissionManager.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "GropPictureView.h"
#import "YWEquipment.h"
#import "YWLocation.h"
#import "YWReportTroubleModel.h"
#import "YYLabel.h"

@interface YWLedgerPostTroubleTableViewCell()
/** 状态Label */
@property (nonatomic, strong) YYLabel *stateLabel;
/** 报障人头像 */
@property (nonatomic, strong) UIImageView *reporterImageView;
/** 报障人 */
@property (nonatomic, strong) YYLabel *reporterName;
/** 任务创建时间 */
@property (nonatomic, strong) YYLabel *createTime;
/** 保障编号 */
@property (nonatomic, strong) YYLabel *bianhao;
/** 设备类型 */
//@property (nonatomic, strong) YYLabel *equipmentLabel;
/** 设备位置 */
//@property (nonatomic, strong) YYLabel *location;
/** 报障描述 */
@property (nonatomic, strong) YYLabel *note;
/** 报障现场图片 */
@property (nonatomic, strong) NSArray *pictures;
/** 图片容器 */
@property (nonatomic, strong) GropPictureView *picturesView;
/** 图片高的约束 */
@property (nonatomic, strong) MASConstraint *picHeightConstraint;
@property (nonatomic, strong) NSString *stateString;

@end

@implementation YWLedgerPostTroubleTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWLedgerPostTroubleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YWLedgerPostTroubleTableViewCellIdentifier];
    if (cell == nil) {
        cell = [[YWLedgerPostTroubleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWLedgerPostTroubleTableViewCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.clipsToBounds = YES;
    }
    return self;
}


- (void)setReportTroulbe:(YWReportTroubleModel *)reportTroulbe{
    _reportTroulbe = reportTroulbe;
    YWHandler *fromUser = nil;
//    NSString *location = nil;
    if (_reportTroulbe.channel == YWPostTroubleTypeEwinApp) {
        fromUser = _reportTroulbe.reporter;
    }else{
        fromUser = [[YWHandler alloc] init];
        fromUser.userName = _reportTroulbe.troubleContact.contactName;
        fromUser.mobile = _reportTroulbe.troubleContact.contactNo;
    }
    
//    if (_reportTroulbe.equipment.equipmentId.length == 0) {
//        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_reportTroulbe.location.locationId];
//    }else{
//        YWEquipment* equipment = _reportTroulbe.equipment;
//        location = [[YWBuildingManager shareManager] equipmentLocation:equipment] ?: @"无";
//    }
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:fromUser.headIcon];
    [_reporterImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    
    NSString *name = fromUser.userName.length?fromUser.userName:@"无";
    _reporterName.text = name;
    
    NSString* reportTimeStr = [YWCommonTool showTimeWithNormalFormatter:_reportTroulbe.reportTime / 1000];
    _createTime.text = reportTimeStr;
    
    NSString*troubleSequence = [NSString stringWithFormat:@"%@",_reportTroulbe.troubleSequence?:@"无"];
    _bianhao.attributedText = [YWCommonTool attributedTextWithFirstText:@"编号: " firstColor:[UIColor customTextGrayBlackColor] secondText:troubleSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    //    [_equipmentLabel labelText:@"设备: " withAttributedText:troubleMission.equipmentName ?:@"无" andAttributedColor:[UIColor blackColor]];
    
    
    //    [_location labelText:@"位置: " withAttributedText:location.length?location:@"无" andAttributedColor:[UIColor blackColor]];
    
    _note.attributedText = [YWCommonTool attributedTextWithFirstText:@"说明: " firstColor:[UIColor customTextGrayBlackColor] secondText:_reportTroulbe.note?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    _pictures = [[YWMissionManager shareManager] picturesWithTroubleMissionId:_reportTroulbe.troubleId pictureTroubleType:YWPictureTroubleTypeMission];
    [_picturesView initWithGroupItems:_pictures withConstraint:nil];
    //任务状态Label
    _stateLabel.text = self.stateString;
    if (_reportTroulbe.troubleStatus == YWTroubleMissionStatusAssign) {
        _stateLabel.textColor = [UIColor customTextGrayColor];
    }else{
        _stateLabel.textColor = [UIColor blackColor];
    }
    
    self.note.preferredMaxLayoutWidth = self.note.frame.size.width;
}

-(NSString *)stateString{
    YWTroubleMissionStatus state = _reportTroulbe.troubleStatus;
    switch (state) {
        case YWTroubleMissionStatusUnAssign:
            _stateString = @"未分配";
            break;
        case YWTroubleMissionStatusAssign:
            _stateString = @"已分配";
            break;
        case YWTroubleMissionStatusFinish:
            _stateString = @"已完成";
            break;
        case YWTroubleMissionStatusCancelPostTrouble:
            _stateString = @"已取消";
            break;
    }
    return _stateString;
}


- (void)setUpSubviews {
    
    UIView *superview = self.contentView;
    /**
     *  头像 名字    状态
     *  头像 时间
     */
    [self.reporterImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(superview).offset(8);
        make.width.and.height.mas_equalTo(@46);
    }];
    
    [self.reporterName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_reporterImageView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_reporterImageView.mas_right).offset(6);
    }];
    
    [self.createTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_reporterName);
        make.top.mas_equalTo(_reporterName.mas_bottom).offset(4);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.baseline.mas_equalTo(_reporterName.mas_baseline);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    /**
     *  编号
     *  设备
     *  位置
     *  说明
     *  图片集
     */
    [self.bianhao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_reporterImageView.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
//    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_bianhao.mas_bottom).offset(6);
//    }];
    
//    [self.location mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_equipmentLabel.mas_bottom).offset(6);
//        
//    }];
    
    [self.note mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bianhao.mas_bottom).offset(6);
        //        make.left.and.right.mas_equalTo(@[_bianhao,_equipmentLabel,_location]);
        //        make.bottom.mas_equalTo(superview).offset(-8);
    }];
    
    [self.picturesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_note.mas_bottom).offset(6).priorityHigh();
        make.left.and.right.mas_equalTo(@[_bianhao,_note]);
        _picHeightConstraint = make.height.mas_equalTo(@90).priorityHigh();
        make.bottom.mas_equalTo(superview).priorityHigh();
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (YYLabel *)stateLabel{
    if (!_stateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        _stateLabel = label;
        [self.contentView addSubview:_stateLabel];
    }
    return _stateLabel;
}

- (UIImageView *)reporterImageView{
    if (!_reporterImageView) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 46/2;
        _reporterImageView = imageView;
        [self.contentView addSubview:_reporterImageView];
    }
    return _reporterImageView;
}

- (YYLabel *)reporterName{
    if (!_reporterName) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customYellowColor];
        _reporterName = label;
        [self.contentView addSubview:_reporterName];
    }
    return _reporterName;
}

- (YYLabel *)createTime{
    if (!_createTime) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _createTime = label;
        [self.contentView addSubview:_createTime];
    }
    return _createTime;
}

- (YYLabel *)bianhao{
    if (!_bianhao) {
        YYLabel *label = [[YYLabel alloc] init];
        _bianhao = label;
        [self.contentView addSubview:_bianhao];
    }
    return _bianhao;
}

//- (YYLabel *)equipmentLabel{
//    if (!_equipmentLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.numberOfLines = 0;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _equipmentLabel = label;
//        [self.contentView addSubview:_equipmentLabel];
//    }
//    return _equipmentLabel;
//}

//- (YYLabel *)location{
//    if (!_location) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.numberOfLines = 0;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _location = label;
//        [self.contentView addSubview:_location];
//    }
//    return _location;
//}

- (YYLabel *)note{
    if (!_note) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _note = label;
        [self.contentView addSubview:_note];
    }
    return _note;
}

-(GropPictureView *)picturesView{
    if (!_picturesView) {
        GropPictureView *gropPictureView = [GropPictureView new];
        _picturesView = gropPictureView;
        [self.contentView addSubview:_picturesView];
    }
    return _picturesView;
}

@end
